cd "${mystart}/Simulated data\Calibration/Benchmark Data/"


* TABLE 1 IN PAPER 	
* DESCRIPTIVE H regressions with income and insurance 
qui{
clear
use "Data files/Benchmark.dta"
keep if age<65
replace age=age-24
replace age_sq=age^2
replace age_cub=age^3

xtile inc_quintile_Y1=income  ,n(5)
label var inc_quintile_Y1 "Income Quintile"
label define inc_qq 1 "Inc Quint = 1st" 2 "Inc Quint = 2nd" 3 "Inc Quint = 3rd" 4 "Inc Quint = 4th" 5 "Inc Quint = 5th"
label values inc_quintile_Y1 inc_qq

label var age "(Age-24)"
label var age_sq "(Age-24)^2"
label var age_cub "(Age-24)^3"
sort ID age
gen H_next = H[_n+1] if age[_n+1]==age+1 & ID==ID[_n+1]

gen I_not_treat_recorded=0 
replace I_not_treat_recorded=1 if I_treat==0 & type2_recorded!=1

gen I_not_treat=0 
replace I_not_treat=1 if I_treat==0 & type2!=1
label var I_not_treat "Not treat shock"

label define lt 1 "Latent health = Good" 2 "Latent health = Bad"
label values latent_health lt


* TABLE 1 IN THE PAPER
gen data=1 
append using "${mystart}\MEPS\MEPS Data Files\Intermediate Data Files\Health_regression.dta"

ologit H_next ib3.H i.education i.dp_record i.du_record   i.ESHI   iB3.inc_quintile  age age_sq age_cub if data==0 //MEPS data     
eststo
ologit H_next ib3.H i.education i.dp_record i.du_record   i.ESHI   iB3.inc_quintile  age age_sq age_cub  if data==1 // NO NO NO
eststo
ologit H_next ib3.H i.education i.dp_record i.du_record   i.ESHI   iB3.inc_quintile  age age_sq age_cub       i.latent_health if data==1  // NO NO YES(latent h)
 eststo
 drop du_record dp_record
 rename dp dp_record
 rename du du_record
 ologit H_next ib3.H i.education i.dp_record i.du_record   i.ESHI   iB3.inc_quintile  age age_sq age_cub     i.I_not_treat if data==1 // yes(true shocks) yes(untreated) no 
 eststo
  ologit H_next ib3.H i.education i.dp_record i.du_record   i.ESHI   iB3.inc_quintile  age age_sq age_cub     i.I_not_treat i.latent_health if data==1 // yes yes yes
 eststo
	esttab using "${out_tables}/Table3.tex", varwidth(25) nogaps   compress label nobaselevels  replace  ///
	nodepvars nonumbers eqlabel(none) noconstant indicate( "Cubic Age = *age* *age_sq* *age_cub*" )  mtitles("Data" "Model (1)" "Model (2)" "Model (3)" "Model (4)") star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) pr2
	eststo clear
	
}


* Table 12 
*TABLE WITH FREQUENCIES OF SHOCKS AGES 45-60, BY HAS ESHI VS UNINSURED
qui{
clear
use "Data files/Benchmark.dta"
drop if H==1
keep if age>=45 & age<=60

label var dp_record "dp recorded"
label var du_record "du recorded"
label var s_record "s recorded"


table INSURANCE  H  ,  statistic(mean dp_record) statistic(mean du_record)  statistic(mean s_record)   nformat(%5.2f)  nototal
table INSURANCE  H  ,  statistic(mean dp) statistic(mean du)  statistic(mean s)   nformat(%5.2f)  nototal
table ESHI  H  ,  statistic(mean dp) statistic(mean du)  statistic(mean s)   nformat(%5.2f)  nototal
table ESHI  H  ,  statistic(fvpercent INSURANCE) 


* Table 12 - middle 2 columns
table INSURANCE  H  if (INSURANCE==1 | INSURANCE==3) ,  statistic(mean dp_record) statistic(mean du_record)  statistic(mean s_record)   nformat(%5.2f)  nototal
collect title "Observed health shock frequencies, ages 45-60, excludes Poor H and those with Public insurance, Model"
collect export "${out_tables}/Avg_shock_freq.tex", tableonly replace

* Table 12 last 2 columns
table INSURANCE  H  if (INSURANCE==1 | INSURANCE==3) ,  statistic(mean dp) statistic(mean du)  statistic(mean s)   nformat(%5.2f)  nototal
collect title "Actual health shock frequencies, ages 45-60, excludes Poor H and those with Public insurance, Model"
collect export "${out_tables}/Avg_shock_freq_actual.tex", tableonly replace

}
	

* Table 17	
* table with R probabilities by insurance 
qui{
	clear
use "Data files/Benchmark.dta"

tab R, nolabel
replace R=R-1
replace R_recorded=R_recorded-1

	* THIS IS JUST THE PROBABILITY OF R (NOT TRANSITION) CONDITIONAL ON INSURANCE
	// MAYBE USE LAGGED r? 
	sort ID age
	gen H_lagged = H[_n-1] if age== age[_n-1] + 1 & ID == ID[_n-1]
logit R_recorded age age_sq age_cub i.H_lagged   i.INSURANCE  if  age<65
eststo margin: margins i.INSURANCE, at(age=45  age_sq=2025 age_cub=91125  ) atmeans post
est sto h1
logit R_recorded age age_sq age_cub   i.H_lagged   i.INSURANCE  if  age<65
eststo margin: margins i.INSURANCE, at(age=60  age_sq=3600 age_cub=216000  ) atmeans post
est sto h2
	esttab h1 h2 using "${out_tables}/R_logit_margins_ins.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("High R, age 45"  "High R, age 60" ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) 
	eststo clear
}

* Table 20, right panel
* Ordered logit, H regression
qui{
clear
use "Data files/Benchmark.dta"
//replace age=age+1
keep if age<65
drop age_sq age_cub
gen age_sq=age^2/100
gen age_cub=age^3/10000

label var age "Age"
label var age_sq "Age sq/100"
label var age_cub "Age cub/10000"

sort ID age
gen H_next = H[_n+1] if age[_n+1]==age+1 & ID==ID[_n+1]

* Table 20
ologit H_next ib3.H age  i.education i.dp_record i.du_record //age_sq age_cub
eststo
	esttab using "${out_tables}/H_initial_reg1.tex", varwidth(25) nogaps   compress label nobaselevels  replace  ///
	nodepvars nonumbers noconstant mtitles("Model" ) star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) pr2
	eststo clear	

}

	
*** Table 22 in appendix - Orgered logit with latent classes, H
qui{
clear
use "Data files/Benchmark.dta"
sort ID age


gen H2= H[_n+1] if age[_n+1]==age+1 & ID==ID[_n+1]
gen H3= H[_n+2] if age[_n+2]==age+2 & ID==ID[_n+2]  
rename H H1

gen du2 = du_record[_n+1] if age[_n+1]==age+1 & ID==ID[_n+1]
gen dp2 = dp_record[_n+1] if age[_n+1]==age+1 & ID==ID[_n+1]  

rename du_record du1
rename dp_record dp1

gen age2= age+1
gen age3= age+2
rename age age1

set seed 339487731
sort ID
sample 1, count by(ID)

keep ID H1 H2 H3  du1 du2 dp1 dp2 age1 age2 age3 education 

drop if H1==. | H2==. | H3==. // making sure everyone has 3 observations
drop if du1==. | dp1==. 
drop if age3>=65 | age1<25

reshape long H  dp du age, i(ID education )
rename _j time

xtset ID age
sort ID age

rename education educ_group
rename age AGE

constraint 1 [H]1L.H#1bn.Class - [H]1L.H#2.Class = 0
constraint 2 [H]2L.H#1bn.Class - [H]2L.H#2.Class = 0
constraint 3 [H]1bn.Class#c.AGE - [H]2.Class#c.AGE = 0
constraint 4 [H]2.educ_group#1bn.Class - [H]2.educ_group#2.Class = 0
constraint 5 [H]3.educ_group#1bn.Class - [H]3.educ_group#2.Class = 0
constraint 6 [H]1L.dp#1bn.Class - [H]1L.dp#2.Class = 0
constraint 7 [H]1L.du#1bn.Class - [H]1L.du#2.Class = 0
constraint 8 [/H]1bn.Class#c.cut1 - [/H]2.Class#c.cut1 = 1
constraint 9 [/H]1bn.Class#c.cut2 - [/H]2.Class#c.cut2 = 1


* Table 22, RHS panel
fmm 2, startvalues(randomid) lcprob(i.educ_group) constraint(1-9) difficult: ologit H Lib3.H AGE   Li.dp Li.du 
eststo
	esttab using "${out_tables}/Tim_logit1.tex", varwidth(25) nogaps   compress label nobaselevels replace  ///
	nodepvars nonumbers eqlabels(" ") mtitles("H"  ) star(* 0.1 ** 0.05 *** 0.01) ///
	 se   b(3) 
	eststo clear
}

* Table 23 and 25
* H TRANSITIONS - by insurance
qui{
clear
use "Data files/Benchmark.dta"
keep if age<65

sort ID age

* deteriorating health
sort ID age
gen H_trans1=0 if H!=1 & ID==ID[_n+1] 
replace H_trans1=1 if ((H==3 & (H[_n+1]==1 | H[_n+1]==2) ) |  (H==2 & H[_n+1]==1 ) ) & ID==ID[_n+1] // 

* STAY IN GOOD H OR IMPROVE
gen H_trans2=0 if ID==ID[_n+1] 
replace H_trans2=1 if ((H==3 & H[_n+1]==3 ) |  (H[_n+1]>H) ) & ID==ID[_n+1] // 

* just stay in good H
gen H_trans3=0 if ID==ID[_n+1] & H==3 & H[_n+1]!=3
replace H_trans3=1 if ID==ID[_n+1] & H==3 & H[_n+1]==3 

* Table 25, RHS
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2 & R_recorded==1
eststo margin: margins i.INSURANCE, at(age=45  age_sq=2025 age_cub=91125   ) atmeans post
est sto h1
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2 & R_recorded==1
eststo margin: margins i.INSURANCE, at(age=55  age_sq=3025 age_cub=166375   ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_R_low.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("Age 45" "Age 55" ) nostar   ///
	se   b(3) 
	eststo clear

* Table 25, LHS
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2 & R_recorded==2
eststo margin: margins i.INSURANCE, at(age=45  age_sq=2025 age_cub=91125   ) atmeans post
est sto h1
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2 & R_recorded==2
eststo margin: margins i.INSURANCE, at(age=55  age_sq=3025 age_cub=166375   ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_R_high.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("Age 45" "Age 55" ) nostar   ///
	se   b(3) 

eststo clear

* Table 23, RHS
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2
eststo margin: margins i.INSURANCE, at(age=45  age_sq=2025 age_cub=91125   ) atmeans post
est sto h1
logit H_trans3 age age_sq age_cub  i.INSURANCE i.education if INSURANCE!=2
eststo margin: margins i.INSURANCE, at(age=55  age_sq=3025 age_cub=166375   ) atmeans post
est sto h2
	esttab h1 h2  using "${out_tables}/H_trans_margins_use.tex", varwidth(25) nogaps   compress label replace  ///
	 nonumbers mtitles("Age 45" "Age 55" ) star(* 0.1 ** 0.05 *** 0.01)   ///
	se   b(3) 
	eststo clear
	
}	
	

* Table 47, bottom panel 	
* Health and income		
qui{
clear
use "Data files/Benchmark.dta"
keep if age<65
reg income i.education i.H age age_sq age_cub 
reg income i.education i.H age age_sq age_cub if emp_ft_pt==2

replace income=income/1000
replace H=2 if H==1

reg income i.H age age_sq age_cub if education==1
eststo
reg income i.H age age_sq age_cub if education==2
eststo
reg income i.H age age_sq age_cub if education==3
eststo
reg income  i.H age age_sq age_cub if emp_ft_pt==2 & education==1
eststo
reg income  i.H age age_sq age_cub if emp_ft_pt==2 & education==2
eststo
reg income  i.H age age_sq age_cub if emp_ft_pt==2 & education==3
eststo
	esttab using "${out_tables}/H_income_corr.tex", varwidth(25) nogaps   compress label nobaselevels indicate( "Cubic Age = *age* *age_sq* *age_cub* " ) replace  ///
	nodepvars nonumbers noconstant mtitles("HS or Less" "Some College" "College" "HS or Less, FT" "Some College, FT" "College, FT") star(* 0.1 ** 0.05 *** 0.01) ///
	se   b(3) pr2
	eststo clear
}
	



	
